package com.mao.server.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mao.server.annotation.HoneyLogs;
import com.mao.server.constant.Constants;
import com.mao.server.domain.enumuric.LogType;
import com.mao.server.domain.po.Notice;
import com.mao.server.domain.po.User;
import com.mao.server.service.NoticeService;
import com.mao.server.service.UserService;
import com.mao.server.utils.BaseContext;
import com.mao.server.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String title) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(title), Constants.TITLE, title);

        Page<Notice> page = noticeService.page(new Page<>(pageNum, pageSize), queryWrapper);

        List<Notice> records = page.getRecords();
        for (Notice record : records) {
            Integer userid = record.getUserid();
            User user = userService.getById(userid);
            if (user != null) {
                record.setUser(user.getName());
            }
        }
        return Result.success(page);
    }

    @HoneyLogs(operation = "公告", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        Long currentId = BaseContext.getCurrentId();
        notice.setUserid(Math.toIntExact(Long.valueOf(currentId)));
        notice.setTime(DateUtil.now());
        noticeService.save(notice);
        return Result.success();
    }

    @HoneyLogs(operation = "公告", type = LogType.UPDATE)

    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return Result.success();
    }

    @HoneyLogs(operation = "公告", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        noticeService.removeById(id);
        return Result.success();
    }

    @HoneyLogs(operation = "公告", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        noticeService.removeBatchByIds(ids);
        return Result.success();
    }


    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        User user = userService.getById(notice.getUserid());
        if (user != null) notice.setUser(user.getName());
        return Result.success(notice);
    }

    @GetMapping("/selectOpenNotice")
    public Result selectOpenNotice() {
        return Result.success(noticeService.list(new QueryWrapper<Notice>().eq("open", 1)));
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.success(noticeService.list());
    }
}